import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/beans/share.dart';
import 'package:flutter_module/beans/shareViewModel.dart';
import 'package:flutter_module/api/statistics_view_model.dart';
import 'package:flutter_module/commom/application.dart';
import 'package:flutter_module/commom/commom.dart';
import 'package:flutter_module/event/deleteShare_event.dart';
import 'package:flutter_module/event/login_event.dart';
import 'package:flutter_module/widget/card/rumor_card.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../DemoLocalizations.dart';

class RumorPage extends StatefulWidget {
  @override
  _RumorPageState createState() => _RumorPageState();
}

class _RumorPageState extends State<RumorPage>
    with AutomaticKeepAliveClientMixin {
  List<dynamic> data = new List();

  bool isReq = false;

  List entriesData = new List();

  StatisticsModel statisticsModel = new StatisticsModel();

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    this.registerLoginEvent();
    getData();
  }

  void registerLoginEvent() {
    Application.eventBus.on<DeleteShare_event>().listen((event) {
      getData();
    });
  }

  @override
  bool get wantKeepAlive => true;

  getData() {
    shareViewModel1.getEntityNews(context).then((v) {
      if (mounted) {
        setState(() {
          data = v;
          isReq = true;
        });
      }
    });
  }

  Future<void> _refreshData() async {
    final Completer<Null> completer = new Completer<Null>();
    getData();
    new Future.delayed(new Duration(seconds: 2), () {
      completer.complete(null);
      _refreshController.refreshCompleted();
    });

    return completer.future;
  }

  Widget buildItem(item) {
    Result model = item;
    return new RumorCard(
      model,
      margin: EdgeInsets.only(
        left: 10.0,
        right: 10.0,
        top: 20.0,
        bottom: 10,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return new Scaffold(
      appBar: AppBar(
        title: Text(DemoLocalizations.of(context).shareList),
        centerTitle: true,
      ),
      body: new SmartRefresher(
        controller: _refreshController,
        onRefresh: _refreshData,
        child: new ListView(
          children: <Widget>[
            new Space(),
            new Space(height: mainSpace / 2),
            new TitleView(DemoLocalizations.of(context).note,
                subTitle: DemoLocalizations.of(context).NumberShares +
                    data.length.toString()),
            new Space(),
            new Divider(
              color: Colors.black,
            ),
            isReq
                ? listNoEmpty(data)
                    ? new Column(
                        children: data.map(buildItem).toList(),
                      )
                    : new Center(
                        child: new Text(
                          DemoLocalizations.of(context).NoShare,
                          style: Theme.of(context).textTheme.display1,
                        ),
                      )
                : new LoadingView(),
          ],
        ),
      ),
    );
  }
}
